package com.example.tijianbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tijianbackend.dto.PersonalOrderDTO;
import com.example.tijianbackend.mapper.*;
import com.example.tijianbackend.pojo.*;
import com.example.tijianbackend.service.CheckitemService;
import com.example.tijianbackend.service.OrdersService;
import com.example.tijianbackend.service.SetmealdetailedService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/17 13:37
 * @version: 1.0
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private SetmealdetailedMapper setmealdetailedMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Autowired
    private CheckitemdetailedMapper checkitemdetailedMapper;


    @Autowired
    private CireportMapper cireportMapper;



    @Autowired
    private CidetailedreportMapper cidetailedreportMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    @Transactional
    public void saveOrders(Orders orders){

        ordersMapper.insert(orders);
        Integer orderId=orders.getOrderId();


        //根据订单套餐smId，查询包含的ciIds列表
        Integer smId=orders.getSmId();
        QueryWrapper queryWrapper1=new QueryWrapper<>().eq("smId",smId);
        List<Setmealdetailed> setmealdetailedList=setmealdetailedMapper.selectList(queryWrapper1);

        List<Integer> ciIds=setmealdetailedList.stream()
                .map(Setmealdetailed::getCiId)
                .collect(Collectors.toList());

        // 查询CiIds中的所有checkitem检查项
        QueryWrapper queryWrapper2= (QueryWrapper) new QueryWrapper().in("ciId",ciIds);
        List<Checkitem> checkitemList=checkitemMapper.selectList(queryWrapper2);
        Map<Integer,Object> map=new HashMap<>();
        for(Checkitem tmp:checkitemList){
            map.put(tmp.getCiId(),tmp);
        }

        // 查询CiIds中的所有checkitemdetailed 中的所有 checkitemdetailed
        QueryWrapper queryWrapper3= (QueryWrapper) new QueryWrapper().in("ciId",ciIds);
        List<Checkitemdetailed> checkitemdetailedList=checkitemdetailedMapper.selectList(queryWrapper3);
        for(Checkitemdetailed tmp:checkitemdetailedList){

            Cidetailedreport cur=new Cidetailedreport(tmp);
            cur.setIsError(0);
            cur.setOrderId(orders.getOrderId());
            cidetailedreportMapper.insert(cur);
        }


        // 插入cireport表
        for(Setmealdetailed setmealdetailed:setmealdetailedList){
            Integer ciId=setmealdetailed.getCiId();

            Cireport cireport=new Cireport();
            cireport.setCiId(ciId);

            Checkitem cur= (Checkitem) map.get(ciId);
            cireport.setCiName(cur.getCiName());
            cireport.setOrderId(orders.getOrderId());

            cireportMapper.insert(cireport);


        }



    }

    @Override
    public List<PersonalOrderDTO> getPersonalOrder(String userId) {

        // 获取order表中符合的userId
        QueryWrapper queryWrapper1=new QueryWrapper<Orders>().eq("userId",userId).orderByDesc("orderDate");
        List<Orders> ordersList=ordersMapper.selectList(queryWrapper1);

        // 收纳smId
        List<Integer> smIds=ordersList.stream()
                .map(Orders::getSmId)
                .collect(Collectors.toList());

        // 查询Setmeal表，查询名字
        QueryWrapper queryWrapper2= (QueryWrapper) new QueryWrapper().in("smId",smIds);

        List<Setmeal> setmealList=setmealMapper.selectList(queryWrapper2);
        Map<Integer,Object> map=new HashMap<>();
        for(Setmeal cur:setmealList){
            map.put(cur.getSmId(),cur);
        }

        List<PersonalOrderDTO> ans=new ArrayList<>();
        for(Orders orders:ordersList){

            PersonalOrderDTO cur=new PersonalOrderDTO();
            cur.setOrderId(orders.getOrderId());
            cur.setOrderDate(orders.getOrderDate());

            cur.setState(orders.getState());
            Setmeal setmeal=(Setmeal)map.get(orders.getSmId());
            cur.setSetmealName(setmeal.getName());

            ans.add(cur);
        }

        return ans;



    }

    @Override
    @Transactional
    public void cancelOrder(Integer orderId) {

        Orders orders=ordersMapper.selectById(orderId);
        orders.setState(3);
        ordersMapper.updateById(orders);

    }


}
